package pwd.allen.queue;

import pwd.allen.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.util.Collections;

/**
 * 生产者发送消息到队列
 */
public class Send {

    private final static String QUEUE_NAME = "simple_queue";

    public static void main(String[] argv) throws Exception {
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 从连接中创建通道，使用通道才能完成消息相关的操作
        Channel channel = connection.createChannel();

        // 声明（创建）队列
        // 需要满足virtual_host配置的configure regexp，否则报错reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'simple_queue' in vhost 'my_vhost' refused for user 'guest', class-id=50, method-id=10
        //durable:持久化  exclusive:排他性，即只对当前连接可见  autoDelete:队列无用时自动删除    arguments:其他参数
        channel.queueDeclare(QUEUE_NAME, false, false, true, Collections.singletonMap("test", "haha"));

        // 消息内容
        String message = "Hello World!";
        // 向指定的队列中发送消息
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        
        System.out.println(" [x] Sent '" + message + "'");

        //关闭通道和连接
        channel.close();
        connection.close();
    }
}